package com.eyeem.recyclerviewtools.sample.xrecyclerview;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eyeem.recyclerviewtools.sample.R;
import com.eyeem.recyclerviewtools.widget.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MultiTypeActivity extends AppCompatActivity implements XRecyclerView.OnRefreshLoadListener {
    private XRecyclerView mRecyclerView;
    private MultiTypeAdapter mAdapter;
    private List<String> listData = new ArrayList<>();
    private int refreshTime = 0;
    private int times = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_type);
        mRecyclerView = (XRecyclerView) this.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        RecycleViewDivider divider = new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, 1, Color.parseColor("#dddddd"));
//        divider.setHeaderDividersEnabled(true);
//        divider.setFooterDividersEnabled(true);
        mRecyclerView.addItemDecoration(divider);
        View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header1, (ViewGroup) findViewById(android.R.id.content), false);
        mRecyclerView.addHeaderView(header);
        header.setBackgroundColor(0xff1874CD);
        View header1 = LayoutInflater.from(this).inflate(R.layout.recyclerview_header1, (ViewGroup) findViewById(android.R.id.content), false);
        mRecyclerView.addHeaderView(header1);
        header1.setBackgroundColor(0xff556B2F);
        View header2 = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup) findViewById(android.R.id.content), false);
        mRecyclerView.addHeaderView(header2);
        header2.setBackgroundColor(0xff1874CD);

        for (int i = 0; i < 50; i++) {
            listData.add("item " + i);
        }

        mAdapter = new MultiTypeAdapter(this, listData);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setRefreshing(true);
        mRecyclerView.setOnRefreshLoadListener(this);
    }

    @Override
    public void onRefresh() {
        refreshTime++;
        times = 0;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                listData.clear();
                for (int i = 0; i < 15; i++) {
                    listData.add("item" + i + "after " + refreshTime + " times of refresh");
                }
                mAdapter.notifyDataSetChanged();
                mRecyclerView.refreshComplete();
            }

        }, 1000);
    }

    @Override
    public void onLoadMore() {
        if (times < 2) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    for (int i = 0; i < 15; i++) {
                        listData.add("item" + (1 + listData.size()));
                    }
                    mRecyclerView.loadMoreComplete();
                    mAdapter.notifyDataSetChanged();
                }
            }, 1000);
        } else {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    for (int i = 0; i < 9; i++) {
                        listData.add("item" + (1 + listData.size()));
                    }
                    mRecyclerView.setNoMore(true);
                    mAdapter.notifyDataSetChanged();
                }
            }, 1000);
        }
        times++;
    }
}
